<?php
namespace Controller;
use Kuyuan\WqCore\controller\Controller;
use Model\ManageModel;

/**
 * 系统管理员
 * Class Enter
 * @package Controller
 */
class Manage extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 新增&&修改
     * @param array $_data
     * @return mixed
     */
    public function handle(Array $_data)
    {
        $_manage_id = ManageModel::where("uid",$_data["uid"])
            ->where("uniacid",$this->_uniacid)
            ->value("id");
        if($_manage_id) {
            $_data["update_time"] = TIMESTAMP;
            $_data["status"] = 1;
            $_result = ManageModel::where("id",$_manage_id)
                ->update($_data);
        } else {
            $_data["uniacid"] = $this->_uniacid;
            $_data["create_time"] = $_data["update_time"] = TIMESTAMP;
            $_result = ManageModel::create($_data);
        }
        return $_result;
    }

    /**
     * 检测用户是不是管理员
     * @param int $_uid 用户id
     * @return mixed
     */
    public function testManage($_uid)
    {
        return ManageModel::where("uid",$_uid)
            ->where("uniacid",$this->_uniacid)
            ->where("status",1)
            ->value("id");
    }

    /**
     * 获取管理员列表
     */
    public function getList()
    {
        return ManageModel::where("uniacid",$this->_uniacid)
            ->where("status",1)
            ->field(array("id","uid","email"))
            ->cache(!KUYUAN_DEBUG)
            ->select();
    }

    /**
     * 删除管理员
     * @param $_manage_id
     * @return mixed
     */
    public function remove($_manage_id)
    {
        return ManageModel::where("id",$_manage_id)
            ->update(array("status"=>0));
    }
}